package com.cloudweb.oa.controller;

import cn.js.fan.util.ErrMsgException;
import cn.js.fan.util.StrUtil;
import com.alibaba.fastjson.JSONObject;
import com.cloudweb.dingding.AttendDingDing;
import com.cloudweb.oa.vo.Result;
import com.cloudwebsoft.framework.util.LogUtil;
import com.redmoon.dingding.service.auth.AuthService;
import com.redmoon.weixin.mgr.AgentMgr;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiResponse;
import io.swagger.annotations.ApiResponses;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.MediaType;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletRequest;
import java.sql.SQLException;
import java.text.ParseException;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;

@RestController
public class DingDingController {

    @Autowired
    HttpServletRequest request;

    com.cloudweb.dingding.AttendDingDing attendDingDing = new AttendDingDing();

    @ApiOperation(value = "登录钉钉", notes = "登录钉钉，用于dd_login.jsp", httpMethod = "POST")
    @ApiResponses({@ApiResponse(code = 200, message = "操作成功")})
    @RequestMapping(value = "/public/dingding/loginByCode", method = RequestMethod.POST, produces = MediaType.APPLICATION_JSON_VALUE)
    public Result<Object> loginByCode() {
        JSONObject json = new JSONObject();
        String skey = "";
        com.redmoon.oa.android.Privilege privilege = new com.redmoon.oa.android.Privilege();
        boolean flag = privilege.authDingDing(request);
        if (flag) {
            skey = privilege.getSkey();
        }
        json.put("skey", skey);
        return new Result<>(json);
    }

    @ApiOperation(value = "获取配置信息", notes = "获取配置信息", httpMethod = "POST")
    @ApiResponses({@ApiResponse(code = 200, message = "操作成功")})
    @RequestMapping(value = "/public/dingding/getConfig", method = RequestMethod.POST, produces = MediaType.APPLICATION_JSON_VALUE)
    public Result<Object> getConfig() {
        String str = AuthService.getConfig(request);
        JSONObject json = JSONObject.parseObject(str);
        return new Result<>(json);
    }

    @ApiOperation(value = "钉钉打卡详情")
    @ApiResponses({@ApiResponse(code = 200, message = "操作成功")})
    @RequestMapping(value = "/public/dingding/getAttendDetails",produces = {"application/json;charset=UTF-8;"})
    @ResponseBody
    public Result<Object> getAttendDetails(
                                           String startWorkDate,
                                           String endWorkDate,
                                           List<String> userIdList,
                                           boolean isI18n,
                                           String statusList
    ) throws ParseException, SQLException {


        attendDingDing.getAttendDetails(startWorkDate, endWorkDate, userIdList, isI18n, statusList);

        if(userIdList == null || userIdList.size() == 0){
            String str = AuthService.getConfig(request);
            JSONObject json = JSONObject.parseObject(str);
            LogUtil.getLog(DingDingController.class).info("json:"+json);
            List<String> userIdList1 = new ArrayList<>();
            return new Result<>(attendDingDing.getAttendDetails(startWorkDate,endWorkDate,userIdList1,isI18n,statusList));
        }else{
            String str = AuthService.getConfig(request);
            JSONObject json = JSONObject.parseObject(str);
            LogUtil.getLog(DingDingController.class).info("json:"+json);
            return new Result<>(attendDingDing.getAttendDetails(startWorkDate,endWorkDate,userIdList,isI18n,statusList));

        }

//        String startWorkDate = "2024-01-06 00:00:00";
//        String endWorkDate = "2024-01-12 23:00:00";
//        List<String> userIdList = new ArrayList<>();
//        boolean isI18n = false;
//        String statusList = "2,3";
//        String str = AuthService.getConfig(request);
//        JSONObject json = JSONObject.parseObject(str);
//        LogUtil.getLog(DingDingController.class).info("json:"+json);
//        return new Result<>(attendDingDing.getAttendDetails(startWorkDate,endWorkDate,userIdList,isI18n,statusList));

    }
}
